package com.cicadasmall.system.wrapper;
import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.common.utils.TreeUtils;
import com.cicadasmall.common.utils.WarpsUtils;
import com.cicadasmall.common.wrapper.EntityWrapper;
import com.cicadasmall.data.domain.RoleDO;
import com.cicadasmall.system.service.IRoleService;
import com.cicadasmall.system.vo.RoleVO;

import java.util.List;


public class RoleWrapper implements EntityWrapper<RoleDO, RoleVO> {

    private final static IRoleService roleService;

    static {
        roleService = SpringContextUtils.getBean(IRoleService.class);
    }

    public static RoleWrapper newBuilder() {
        return new RoleWrapper();
    }

    @Override
    public RoleVO entityVO(RoleDO entity) {
        RoleVO roleVo = WarpsUtils.copyTo(entity, RoleVO.class);
        assert roleVo != null;
        if (Fn.isNotNull(roleVo.getParentId()) && Fn.notEqual(Constant.PARENT_ID, roleVo.getParentId())) {
            RoleDO parentRole = roleService.getById(entity.getParentId());
            roleVo.setParentName(parentRole.getRoleName());
        } else {
            roleVo.setParentName("顶级类目");
        }

        return roleVo;
    }

    @Override
    public List<RoleVO> treeVO(List<RoleDO> list) {
        return TreeUtils.toTree(this.listVO(list));
    }
}
